import { defineStore } from 'pinia';
export const date = defineStore('date', {
	state: () => {
		return {
			// name: '李浩明',
			// age: 23,
		};
	},
	getters: {
		// changeAge() {
		// 	return this.age + 1;
		// },
	},
	actions: {
		getDaysBetweenDates(date1, date2) {
			// 将日期字符串转换为时间戳
			var timestamp1 = new Date(date1).getTime();
			var timestamp2 = new Date(date2).getTime();

			// 计算两个时间戳之间的天数
			var days = Math.ceil(
				Math.abs(timestamp2 - timestamp1) / (1000 * 3600 * 24)
			);

			return days;
		},
		getNowDate(date = null) {
			let _d = date;
			if (_d === null) {
				_d = new Date();
			} else {
				if (typeof date === 'string') {
					try {
						_d = new Date(JSON.parse(_d));
					} catch (error) {
						_d = new Date(_d);
					}
				}
			}
			let _year = String(_d.getFullYear());
			let _month = String(_d.getMonth() + 1);
			let _days = String(_d.getDate());
			return _year + '.' + _month + '.' + _days;
		},
	},
});
